import { onMounted, ref } from "vue";
import { getTopCategoryAPI } from "@/api/category.js";
import { onBeforeRouteUpdate, useRoute } from "vue-router";


export const useCategory = () => {
    const route = useRoute();
    const categoryData = ref([]);
    const getCategory = async (id = route.params.id) => {
        const result = await getTopCategoryAPI(id);
        categoryData.value = result.result;
    };
    onMounted(() => {
        getCategory();
    });
    //目标：路由参数变化的时候 可以把分类数据接口重新发送
    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id)
    })

    return {
        categoryData
    }
}